<template>
  <div>
    <div class="flex-center mb16">
      <Title title="账号信息"/>
      <el-button v-if="!id" class="empty-btn-style" size="small" type="info" @click="handleQuote">引用账号</el-button>
    </div>
    <el-form
      ref="accountForm"
      :rules="rules"
      :model="paramsObj"
      :label-position="showMobilePage?'left':'right'"
      class="lineHeight32"
      label-width="110px">
      <el-row class="response-el-row" type="flex">
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="平台" prop="platform">
            <el-select v-model="paramsObj.platform" :disabled="Boolean(id)" placeholder="请选择" class="width240" size="small" @change="changePlatform">
              <el-option
                v-for="item in platformOption"
                :key="item.id"
                :label="item.value"
                :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="主页链接" prop="link">
            <el-input v-model="paramsObj.link" :maxlength="200" class="width240" placeholder="请输入主页链接" size="small" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="账号名称" prop="accountName">
            <el-input v-model="paramsObj.accountName" :maxlength="40" class="width240" placeholder="请输入账号名称" size="small" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="账号ID" prop="accountId">
            <el-input v-model="paramsObj.accountId" :disabled="Boolean(id)" :maxlength="40" class="width240" placeholder="请输入账号ID" size="small" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="达人所在城市" required>
            <three-level-area
              :selected-options="handelArr(paramsObj.area)"
              :class="!showMobilePage && 'width240'"
              class="height32"
              clearable
              select-type="city"
              placeholder="省-市"
              @update:options="threeLevelArea($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="标签" prop="tag">
            <template slot="label">
              <span style="display: inline-flex;align-items: center;">标签
                <InfoIcon content="标签最多可选择3个" style="width: 16px;height: 16px;margin-left: 4px;" />
              </span>
            </template>
            <TagSelect v-model="paramsObj.tag" :class="!showMobilePage && 'width240'" :module-type="TAG_MODULES.FILTER" :platform="paramsObj.platform" need-plat-form-params limit class="tag-select" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="粉丝量" prop="fans">
            <el-input v-model="paramsObj.fans" :maxlength="9" class="width240" placeholder="请输入粉丝量" size="small" @input="numInputChange($event, 'fans')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="点赞数" prop="like">
            <el-input v-model="paramsObj.like" :maxlength="9" class="width240" placeholder="请输入点赞数" size="small" @input="numInputChange($event, 'like')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="评论数" prop="comment">
            <el-input v-model="paramsObj.comment" :maxlength="9" class="width240" placeholder="请输入评论数" size="small" @input="numInputChange($event, 'comment')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8">
          <el-form-item label="收藏数" prop="collect">
            <el-input v-model="paramsObj.collect" :maxlength="9" class="width240" placeholder="请输入收藏数" size="small" @input="numInputChange($event, 'collect')"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="responsive-col-lg-8 sex">
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="paramsObj.gender">
              <el-radio :label="0">男</el-radio>
              <el-radio :label="1">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <AccountSelection
      ref="accountSelectionRef"
      :visible.sync="showAccountSelection"
      :multiple="false"
      :expand-params = "{ status: 1 }"
      :expend-filter="[{ keyword: 'operator', name: '供应商名称', type: 'input', labelWidth: '99px' }]"
      @submit="handleAccountSelected" >
      <template slot="tableColumn">
        <AccountColumn />
      </template>
    </AccountSelection>
  </div>
</template>

<script>
import Title from '@/components/Title';
import ThreeLevelArea from '@/components/ThreeLevelArea';
import AccountSelection from '@/components/resource/AccountSelection';
import TagSelect from '@/components/Common/tagSelect';
import AccountColumn from './AccountColumn';
import { mapState } from 'vuex';
import { TAG_MODULES } from '@/utils/const';

export default {
  components: { Title, ThreeLevelArea, AccountSelection, AccountColumn, TagSelect },
  model: {
    prop: 'paramsObj',
    event: 'change'
  },
  props: {
    paramsObj: {
      type: Object,
      default() {
        return {};
      }
    },
    id: {
      type: Number | String,
      default: 0
    },
    platformOption: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      TAG_MODULES,
      rules: {
        'platform': [{ required: true, message: '请选择平台', trigger: 'change' }],
        'gender': [{ required: true, message: '请选择性别', trigger: 'change' }],
        'area': [{ required: true, message: '请选择达人所在城市', trigger: 'change' }],
        // 'tag': [{ required: true, message: '请选择标签', trigger: 'change' }],
        'tag': [{ required: true, message: '请选择标签', trigger: 'blur' }],
        'link': [{ required: true, message: '请输入主页链接', trigger: 'blur' }],
        'accountName': [{ required: true, message: '请输入账号名称', trigger: 'blur' }],
        'accountId': [{ required: true, message: '请输入账号ID', trigger: 'blur' }],
        'fans': [{ required: true, message: '请输入粉丝量', trigger: 'blur' }]
      },
      showAccountSelection: false,
      quoteData: {}
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  methods: {
    numInputChange(text, prop) {
      let str;
      str = text.replace(/[^\d.]/g, '');
      if (prop === 'offer' || prop === 'notReportPrice') {
        str = str.replace(/\.{2,}/g, '.'); // 连续两个小数点 replace为.
        str = str.replace(/^\./g, '0.'); // 已点为开头 replace为 0.
        str = str.replace(/^(\d*)\.(\d*)\./g, '$1.$2');// 不连续点点 删掉
        str = str.replace(/^0[^\.]+/g, '0'); // 连续多个0 replace为1个0
        str = str.replace(/^(\-?\d+)\.(\d\d).*$/, '$1.$2'); // 只允许保留小数点后2位
      } else {
        str = str.replace(/\.|^0/g, ''); // 数量只允许输入正整数
      }
      str = str === '' ? null : str;
      this.paramsObj[prop] = str;
    },
    changePlatform() {
      this.$set(this.paramsObj, 'tag', []);
      // this.paramsObj.tag = [];
    },
    handelArr(data) {
      if (!data) return [];
      if (typeof data === 'string') {
        return data.split(',');
      } else {
        return data;
      }
    },
    // 选择城市
    threeLevelArea(data) {
      this.paramsObj.area = data.join(',');
      this.$emit('changeValue', this.paramsObj);
    },
    validateForm(validate = true, index = 0) {
      return new Promise((resolve, reject) => {
        if (validate) {
          this.$refs.accountForm.validate(valid => {
            if (valid) {
              resolve(valid);
            } else {
              reject(index);
            }
          });
        } else {
          resolve(true);
        }
      });
    },
    handleQuote() {
      this.$refs['accountSelectionRef'].init(this.quoteData);
      this.showAccountSelection = true;
    },
    handleAccountSelected(data) {
      this.quoteData = data;
      this.$emit('selectAccount', data);
      this.showAccountSelection = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.mb16 {
  margin-bottom: 16px;
}
.mb32 {
  ::v-deep {
    .el-form-item {
      margin-bottom: 32px;
    }
  }
}
::v-deep {
  .el-cascader {
    line-height: 32px;
  }
  .el-radio__label {
    padding-left: 4px;
  }
}
.width240 {
  width: 240px;
  &::v-deep{
    .el-tag.el-tag--info {
      color: #406eff;
      background: #eff3fb;
      display: flex;
      align-items: center;
      .el-tag__close {
        right: 0;
        margin: 0 0 0 3px;
      }
    }
  }
}
.empty-btn-style {
  border-color: #406EFF;
  background: #fff;
  color: #406EFF;
  width: 80px;
  padding: 7px 0;
  text-align: center;
}
.sex {
  ::v-deep {
    .el-radio__label {
      font-size: 14px;
    }
  }
}
.tag-select {
  ::v-deep {
    .el-cascader {
      width: 100%;
    }
  }
}
</style>
